﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Security;
using Model.Entity;
using Ones.Dal.Repository.Impl;

namespace Ones.Wcf.SystemAdministration
{
    public class ServiceAccountManagement : IServiceAccountManagement
    {
        private IEmployeeDao _daoEmp;
        private IMembershipUserEmployeeDao _daoMued;
        private IViewMembershipUserEmployeeDao _daoView;

        #region Constructor
 
        public ServiceAccountManagement()
        {
            _daoEmp = new EmployeeDao();
            _daoMued = new MembershipUserEmployeeDao();
            _daoView = new ViewMembershipUserEmployeeDao();
        }

        #endregion

        #region DMM

        public bool
        createUserAccount(string username, string password, string email, string empId, string role)
        {
            try
            {
                MembershipUser user = Membership.CreateUser(username, password, email);
                _daoEmp.addAccountToEmployee(empId, (Guid)user.ProviderUserKey);
                Roles.AddUserToRole(username, role);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool
        modifyAccount(Employee modifiedEmp)
        {
            try
            {
                _daoEmp.update(modifiedEmp);
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return false;
            }
        }

        public bool
        createRole(string roleName)
        {
            try
            {
                Roles.CreateRole(roleName);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool
        lockUser(object userId)
        {
            MembershipUser user = Membership.GetUser(new Guid(userId.ToString()));
            if (user != null)
            {
                user.IsApproved = false;
                Membership.UpdateUser(user);
                return true;
            }
            return false;
        }

        public bool
        unlockUser(object userId)
        {
            MembershipUser user = Membership.GetUser(new Guid(userId.ToString()));
            if (user != null)
            {
                user.IsApproved = true;
                Membership.UpdateUser(user);
                return true;
            }
            return false;
        }

        public bool
        updateAccountDetails(string empId, string userId, string username, string fname, string lname, bool isMale, string email, string role, bool isApproved)
        {
            try
            {
                MembershipUser user = Membership.GetUser(new Guid(userId));
                user.IsApproved = isApproved;
                user.Email = email;
                Membership.UpdateUser(user);
                Roles.RemoveUserFromRoles(username, Roles.GetRolesForUser(username));
                Roles.AddUserToRole(username, role);
                _daoMued.updateAccountDetails(empId, fname, lname, isMale);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool
        changePassword(string userId, string oldPwd, string newPwd)
        {
            try
            {
                return Membership.GetUser(new Guid(userId)).ChangePassword(oldPwd, newPwd);
            }
            catch (Exception)
            {
                return false;
            }
        }

        public string
        resetPassword(string userId)
        {
            try
            {
                return Membership.GetUser(new Guid(userId)).ResetPassword();
            }
            catch (Exception)
            {
                return null;
            }
        }

        #endregion

        #region DQM

        public List<Employee>
        getEmployees()
        {
            try
            {
                return _daoEmp.getAll();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<ViewMembershipUserEmployee>
        getEmployeesHaveAccount()
        {
            try
            {
                return _daoView.getAll();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<Employee>
        getEmployeesNoAccount()
        {
            return _daoEmp.getMultiple(i => i.userId == null);
        }

        public List<ViewMembershipUserEmployee>
        searchEmpHaveAcc(string searchBy, string key)
        {
            try
            {
                List<ViewMembershipUserEmployee> lst = null;
                if (searchBy == "empId")
                {
                    lst = _daoView.getMultiple(e => e.empId.Contains(key));
                }
                else if (searchBy == "name")
                {
                    lst = _daoView.getMultiple(e => (e.empFirstName + " " + e.empLastName).Contains(key) || e.UserName.Contains(key));
                }
                return lst;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<Employee>
        searchEmpNoAcc(string searchBy, string key)
        {
            try
            {
                List<Employee> lst = null;
                if (searchBy == "empId")
                {
                    lst = _daoEmp.getMultiple(i => i.empId.Contains(key) && i.userId.Value == null);
                }
                else if (searchBy == "name")
                {
                    lst = _daoEmp.getMultiple(i => (i.empFirstName + " " + i.empLastName).Contains(key) && i.userId.Value == null);
                }
                return lst;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public ViewMembershipUserEmployee
        getAccountDetails(object userId)
        {
            return _daoMued.getAccountDetails(userId);
        }
        
        public Employee
        getEmployeeById(string empId)
        {
            return _daoEmp.getById(empId);
        }

        public List<aspnet_Role>
        getRoles()
        {
            return (new RoleDao()).getAll();
        }
        
        #endregion DMM
    }
}